﻿''' <summary>ColorDialog</summary>
''' <author>Comentsys</author>
''' <version>1.0.0</version>
''' <created>11th February 2009</created>
''' <modified>23rd February 2009</modified>
''' <remarks>ColorDialog</remarks>
Partial Public Class ColorDialog
    ' Private Members

    Private _Color As Color
    Private _Alpha As Byte = 255
    Private _Red As Byte = 255
    Private _Green As Byte = 255
    Private _Blue As Byte = 255
    Private _Presets As New Dictionary(Of String, Color)
    Private blnSelected As Boolean = False

    ' Private Methods

    ''' <summary>Update</summary>
    ''' <remarks>Updates Color Settings</remarks>
    Private Sub Update()
        AlphaValue.Text = _Alpha
        RedValue.Text = _Red
        GreenValue.Text = _Green
        BlueValue.Text = _Blue
        _Color = Color.FromArgb(_Alpha, _Red, _Green, _Blue)
        Preview.Fill = New SolidColorBrush(Color.FromArgb(_Alpha, _Red, _Green, _Blue))
    End Sub

    ' Public Properties

    ''' <summary>Color</summary>
    ''' <value>Color</value>
    ''' <returns>Selected Color</returns>
    ''' <remarks>Dialog Property</remarks>
    Public Property Color() As Color
        Get
            Return Color.FromArgb(_Alpha, _Red, _Green, _Blue)
        End Get
        Set(ByVal Value As Color)
            _Alpha = Value.A
            _Red = Value.R
            _Green = Value.G
            _Blue = Value.B
            A.Value = _Alpha
            R.Value = _Red
            G.Value = _Green
            B.Value = _Blue
            Update()
        End Set
    End Property

    ''' <summary>Alpha</summary>
    ''' <value>Integer</value>
    ''' <returns>Colour Alpha Value</returns>
    ''' <remarks>Colour Component</remarks>
    Public Property Alpha() As Integer
        Get
            Return _Alpha
        End Get
        Set(ByVal Value As Integer)
            _Alpha = Value
            Update()
        End Set
    End Property

    ''' <summary>Red</summary>
    ''' <value>Integer</value>
    ''' <returns>Colour Red Value</returns>
    ''' <remarks>Colour Component</remarks>
    Public Property Red() As Integer
        Get
            Return _Red
        End Get
        Set(ByVal Value As Integer)
            _Red = Value
            Update()
        End Set
    End Property

    ''' <summary>Green</summary>
    ''' <value>Integer</value>
    ''' <returns>Colour Green Value</returns>
    ''' <remarks>Colour Component</remarks>
    Public Property Green() As Integer
        Get
            Return _Green
        End Get
        Set(ByVal Value As Integer)
            _Green = Value
            Update()
        End Set
    End Property

    ''' <summary>Blue</summary>
    ''' <value>Integer</value>
    ''' <returns>Colour Blue Value</returns>
    ''' <remarks>Colour Component</remarks>
    Public Property Blue() As Integer
        Get
            Return _Blue
        End Get
        Set(ByVal Value As Integer)
            _Blue = Value
            Update()
        End Set
    End Property

    ''' <summary>Value</summary>
    ''' <value>String</value>
    ''' <returns>String</returns>
    ''' <remarks>Internal Storage as Color</remarks>
    Public Property Value() As String
        Get
            Return ZuneCardr.Settings.SerialiseColour(Color)
        End Get
        Set(ByVal Value As String)
            Color = ZuneCardr.Settings.DeserialiseColour(Value)
        End Set
    End Property

    ''' <summary>ShowDialog</summary>
    ''' <param name="Title">Title</param>
    ''' <remarks>Show Dialog with Title and Message Provided</remarks>
    Public Overloads Sub ShowDialog(ByRef Title As String)
        Selected = False
        Me.Title = Title ' Set the Title
        cboPresets.ItemsSource = Presets ' Set Preset List
        MyBase.ShowDialog()
    End Sub

    ''' <summary>Selected</summary>
    ''' <value>Boolean</value>
    ''' <returns>True if Selected, False if Not / Cancel</returns>
    ''' <remarks>Indicates a Colour has been Selected</remarks>
    Public Property Selected() As Boolean
        Get
            Return blnSelected
        End Get
        Set(ByVal Value As Boolean)
            blnSelected = Value
        End Set
    End Property

    ''' <summary>Presets</summary>
    ''' <value>System.Reflection.PropertyInfo</value>
    ''' <returns>System.Reflection.PropertyInfo</returns>
    ''' <remarks>Colour Preset Property Information</remarks>
    Public Property Presets() As Dictionary(Of String, Color)
        Get
            Return _Presets
        End Get
        Set(ByVal Value As Dictionary(Of String, Color))
            _Presets = Value
        End Set
    End Property

    ' Form Methods

    Private Sub cboPresets_SelectionChanged(ByVal sender As System.Object, _
                                            ByVal e As System.Windows.Controls.SelectionChangedEventArgs) _
                                            Handles cboPresets.SelectionChanged
        Color = Presets.Item(CType(cboPresets.SelectedItem,  _
                             KeyValuePair(Of String, Color)).Key)
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, _
                     ByVal e As System.Windows.RoutedEventArgs) Handles OK.Click
        Selected = True
        Close()
    End Sub

    Private Sub Window_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        GetType(Window).GetField("_isClosing", Reflection.BindingFlags.Instance Or _
                                 Reflection.BindingFlags.NonPublic).SetValue(Me, False)
        e.Cancel = True
        Me.Hide()
    End Sub

End Class
